package com.workingpub.commons.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 一些常用的方法
 * 
 * @author anybody
 *
 */
public class UsualUtils {

	public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat DEFAULT_DATETime_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final DecimalFormat DEFAULT_INTEGER_FORMAT = new DecimalFormat("############################");
	public static final DecimalFormat DEFAULT_DOUBLE_FORMAT = new DecimalFormat(
			"############################.############################");

	/**
	 * 将未采用泛型的Map Object value值转化为String
	 * 
	 * @param obj
	 *            Map中的值
	 * @return String
	 */
	public static final String converMapObjToStr(Object obj) {
		String value = null;
		value = obj != null ? obj.toString() : null;
		return value;
	}

	/**
	 * 判断对象是否为空
	 * 
	 * @param obj
	 * @return
	 */
	public static final boolean isEmpty(Object obj) {
		if (null == obj) {
			return true;
		} else {
			if (obj instanceof String || obj instanceof StringBuilder || obj instanceof StringBuffer
					|| obj instanceof java.io.StringWriter) {
				return obj.toString().trim().length() < 1;
			} else {
				return false;
			}
		}
	}

	/**
	 * 对象是否不为空
	 * 
	 * @param obj
	 * @return
	 */
	public static final boolean notEmpty(Object obj) {
		return false == isEmpty(obj);
	}

	/**
	 * 转换为日期字符串
	 * 
	 * @param value
	 * @param strFormat
	 * @return
	 */
	public static final String toDateString(Object value, String strFormat) {
		SimpleDateFormat df = isEmpty(strFormat) ? DEFAULT_DATE_FORMAT : new SimpleDateFormat(strFormat);
		return df.format((Date) value);
	}

	public static final String formatDate(Date date) {
		return DEFAULT_DATE_FORMAT.format(date);
	}

	public static final String formatDatetime(Date date) {
		return DEFAULT_DATETime_FORMAT.format(date);
	}

	/**
	 * 转换为数字字符串
	 * 
	 * @param value
	 * @param strFormat
	 * @return
	 */
	public static final String toIntegerString(Object value, String strFormat) {
		DecimalFormat sf = isEmpty(strFormat) ? DEFAULT_INTEGER_FORMAT : new DecimalFormat(strFormat);
		return sf.format(value);
	}

	/**
	 * 转换为有小数的数字字符串
	 * 
	 * @param value
	 * @param strFormat
	 * @return
	 */
	public static final String toDoubleString(Object value, String strFormat) {
		DecimalFormat sf = isEmpty(strFormat) ? DEFAULT_DOUBLE_FORMAT : new DecimalFormat(strFormat);
		return sf.format(value);
	}

	/**
	 * 转换为数组字符串
	 * 
	 * @param value
	 * @param strFormat
	 * @return
	 */
	public static final String toArrayString(Object value, String strFormat) {
		return toArray(value, true).toString();
	}

	/**
	 * 对象转换为数组
	 * 
	 * @param value
	 * @param doesConvertCollection
	 * @return
	 */
	public static final Object[] toArray(Object value, boolean doesConvertCollection) {
		if (null == value) {
			return null;
		}
		Object[] ret = null;
		if (doesConvertCollection && java.util.Collection.class.isAssignableFrom(value.getClass())) {
			java.util.Collection<?> cxs = (java.util.Collection<?>) value;
			Object[] objs = new Object[cxs.size()];
			cxs.toArray(objs);
			ret = objs;
		} else if (java.util.Enumeration.class.isAssignableFrom(value.getClass())) {
			java.util.Enumeration<?> e = (java.util.Enumeration<?>) value;
			java.util.ArrayList<Object> r = new java.util.ArrayList<Object>();
			while (e.hasMoreElements()) {
				r.add(e.nextElement());
			}
			Object[] objs = new Object[r.size()];
			r.toArray(objs);
			return objs;
		} else if (value.getClass().isArray()) {
			Class<?> type = value.getClass().getComponentType();
			Object[] objs = null;
			if (type == Boolean.TYPE) {
				boolean[] bs = (boolean[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Boolean.valueOf(bs[n]);
				}
			} else if (type == Byte.TYPE) {
				byte[] bs = (byte[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Byte.valueOf(bs[n]);
				}
			} else if (type == Short.TYPE) {
				short[] bs = (short[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Short.valueOf(bs[n]);
				}
			} else if (type == Integer.TYPE) {
				int[] bs = (int[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Integer.valueOf(bs[n]);
				}
			} else if (type == Long.TYPE) {
				long[] bs = (long[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Long.valueOf(bs[n]);
				}
			} else if (type == Float.TYPE) {
				float[] bs = (float[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Float.valueOf(bs[n]);
				}
			} else if (type == Double.TYPE) {
				double[] bs = (double[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Double.valueOf(bs[n]);
				}
			} else if (type == Character.TYPE) {
				char[] bs = (char[]) value;
				objs = new Object[bs.length];
				for (int n = 0; n < bs.length; n++) {
					objs[n] = Character.valueOf(bs[n]);
				}
			} else {
				objs = (Object[]) value;
			}
			ret = objs;
		}
		return ret;
	}

	/**
	 * 填充字符串<br>
	 * <blockquote> 需要重复的字符串repeatStr，为空则不重复，相当于start+end;<br>
	 * 需要重复的总长度repeatLen，大于0才能重复;<br>
	 * 开始字符串start，可为空;<br>
	 * 结束字符串end，可为空; </blockquote>
	 * 
	 * @param start
	 * @param end
	 * @param repeatStr
	 * @param repeatLen
	 * @return
	 */
	public static final String fillString(String repeatStr, int repeatLen, String start, String end) {
		String repeat = "";
		if (repeatLen > 0 && notEmpty(repeatStr)) {
			int repeatSize = repeatStr.length();
			for (int i = 0; i < repeatLen / repeatSize; i++) {
				int lastLen = repeatLen - repeatSize * i;
				if (lastLen < repeatSize) {
					repeat += repeatStr.substring(0, lastLen);
				} else {
					repeat += repeatStr;
				}
			}
		}
		return start + repeat + end;
	}
}
